package com.creatorblue.modules.job;

import org.quartz.*;

import java.util.Date;

/**
 * @author huj
 * @create 2022-01-15 9:29
 * @email hujun@creatorblue.com
 */
@PersistJobDataAfterExecution
public class ActivityJob implements Job {

    private Integer count;

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        System.out.println("Hello, everybody 当前时间是 " + new Date());

        // 获取Trigger
        Trigger trigger = jobExecutionContext.getTrigger();
        // 获取JobDetail
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        // 获取Scheduler
        Scheduler scheduler = jobExecutionContext.getScheduler();

        // 获取Trigger名字
        String name = trigger.getKey().getName();
        // 获取Trigger组名（默认为 DEFAULT）
        String group = trigger.getKey().getGroup();

        // 触发器触发的预定时间
        Date scheduledFireTime = jobExecutionContext.getScheduledFireTime();
        // 实际触发时间。例如，计划时间可能是 10:00:00，但如果调度程序太忙，实际触发时间可能是 10:00:03
        Date fireTime = jobExecutionContext.getFireTime();
        // 上次触发时间
        Date previousFireTime = jobExecutionContext.getPreviousFireTime();
        // 下次触发时间
        Date nextFireTime = jobExecutionContext.getNextFireTime();

        System.out.println("获取Trigger名字 => " + name);
        System.out.println("获取Trigger组名 => " + group);
        System.out.println("触发器触发的预定时间 => " + scheduledFireTime);
        System.out.println("实际触发时间 => " + fireTime);
        System.out.println("上次触发时间 => " + previousFireTime);
        System.out.println("下次触发时间 => " + nextFireTime);

        System.out.println(++count);
        jobExecutionContext.getJobDetail().getJobDataMap().put("count", count);
    }

}
